package com.topfinance.cnaps2.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.topfinance.cnaps2.dto.ConfOperationDto;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.topfinance.payment.dto.TCfgOperationDto;
import com.topfinance.payment.ebo.TCfgOperationEbo;
import com.topfinance.payment.service.ITCfgOperationMgr;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class CfgOperAction extends BaseSupport implements ModelDriven<TCfgOperationDto> {

	private ITCfgOperationMgr operManager = null;

	private TCfgOperationDto operDto = new TCfgOperationDto();

	public TCfgOperationDto getModel() {
		return operDto;
	}

	public void setCfgOperationMgr(ITCfgOperationMgr operManager) {
		this.operManager = operManager;
	}

	public CfgOperAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String list() {
		log.debug("CfgOperAction-list begin");
		try {
			List<TCfgOperationEbo> l = operManager.getAllOper();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgOperAction-list end");
		return SUCCESS;
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(operDto));
			TCfgOperationEbo o = operManager.addOper(operDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + o.getOid();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			e.printStackTrace();
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(operDto));
			operManager.editOper(operDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			String delId = request.getParameter("id");
			operManager.deleteOper(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}
	
	public TCfgOperationDto getCfgOperPortInfoEbo() {
		return operDto;
	}

	public void setCfgOperPortInfoEbo(TCfgOperationDto operDto) {
		this.operDto = operDto;
	}

	public String query() {
		if (request.getParameter("uid") == null) {
			rtnResultMap = new HashMap();
			String[] args = new String[1];
			args[0] = "";// you can add extra message here
			rtnResultMap.put("info", getText("expect.valid.pk.data", args));
			return SUCCESS;
		}
		try {
			String uid = request.getParameter("uid");
			System.out.println("[OID]" + uid);
			TCfgOperationEbo trans = operManager.getOper(new Integer(uid));
			List l = new ArrayList<TCfgOperationEbo>();
			l.add(trans);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, 1);
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	
	
	public String test() {
		log.debug("ConfOper-test begin");
		try {
			TCfgOperationDto oDto = new TCfgOperationDto();
			oDto.setName("ibps.102.01.02");
//			oDto("ACK_MSG_DW");
//			oDto.setAckMsgUp("ACK_MSG_UP");
//			oDto.setConvertTmplDw("");
//			oDto.setConvertTmplUp("");
//			oDto.setIsAckDw("Y");
//			oDto.setIsAckUp("N");
//			oDto.setIsPermitDw("Y");
//			oDto.setIsPermitUp("Y");
			//oDto.setPID(1021);
			operManager.addOper(oDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("info", getText("create.success", "Test Success!"));
			return SUCCESS;
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("ConfOper-list end");
		return SUCCESS;
	}

}
